package com.itheima.youdianda.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.domain.YddAdvimg;
import com.itheima.youdianda.domain.YddAdvpos;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.mapper.YddAdvposMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YUAN
 * @description 针对表【ydd_advpos】的数据库操作Service实现
 * @createDate 2022-09-06 18:36:39
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos>
        implements YddAdvposService {

    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
     * 广告位列表  -- 分页
     *
     * @return
     */
    @Override
    public Result advList(Integer page, Integer limit, String advposdesc, String advposname) {
        Page ipage = new Page(page, limit);

        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);//模糊
        lqw.like(StrUtil.isNotBlank(advposname), YddAdvpos::getAdvposname, advposname);//模糊

        ipage = page(ipage, lqw);

        List<YddAdvpos> records = ipage.getRecords();
        records.stream().map(yddAdvpos -> {
            //拿到该广告位id对应的广告图的数量
            LambdaQueryWrapper<YddAdvimg> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(YddAdvimg::getAdvimgpos, yddAdvpos.getId());
            List<YddAdvimg> list = yddAdvimgService.list(lqw2);
            yddAdvpos.setAdvposnum(list.size());
            return yddAdvpos;
        }).collect(Collectors.toList());

        //用map集合处理下参数
        Map<String, Object> map = new HashMap<>();
        map.put("data", ipage.getRecords());
        map.put("count",ipage.getTotal());
        map.put("totalPages",ipage.getPages());
        map.put("pageSize",ipage.getSize());
        map.put("currentPage",ipage.getCurrent());


        return Result.ok(map);
    }

    /**
     * 广告位修改
     *
     * @param yddAdvpos
     * @return
     */
    @Override
    public Result updateAdvPos(YddAdvpos yddAdvpos) {
        updateById(yddAdvpos);
        return Result.ok("修改成功!");
    }
}




